<?php
include "bootstrap.php";

include "services/FlextrineService.php";

$a = new FlextrineService();
$em = $a->getEntityManager();

$a->useFixture("garden1");

// trying to get trees in garden 1
//$query = $em->createQuery("SELECT parentEntity, assocations FROM garden\Tree parentEntity JOIN parentEntity.branches assocations");
//$query = $em->createQuery("SELECT t FROM garden\\Tree t JOIN t.garden g WHERE g.id=1");
//var_dump($query->getResult());

//$garden = $em->getRepository("\garden\Garden")->find(1);
//$garden->trees->get(0);

//var_dump($garden->trees->getValues());

$garden = new \garden\Garden(); $garden->id = 1; $garden->name = "Garden 0";
$mergedGarden = $em->merge($garden);
$mergedGarden->name = "Changed";
$em->flush();
return;

/*$d1 = new \vo\Doctor(); $d1->name = "Doctor 1";

$p1 = new \vo\Patient(); $p1->name = "Patient 1";
$p2 = new \vo\Patient(); $p2->name = "Patient 2";

$d1->patients->add($p1); $p1->doctor = $d1;
$d1->patients->add($p2); $p2->doctor = $d1;

$em->persist($d1);
$em->persist($p1);
$em->persist($p2);
$em->flush();

$d1->patients->clear();
$p1->doctor = null;
$p2->doctor = null;
$em->flush();*/

/*for ($n = 1; $n <= 4; $n++) {
	$patient = new \vo\Patient();
	$patient->id = $n;
	$patient->name = "Patient $n";
	$patient->doctor = null;
	
	$em->merge($patient);
}*/

$d1 = $em->getRepository("vo\Doctor")->find(1);
$em->remove($d1);

$em->flush();

return;

// These are detached, but are definitely in the database
/*$p1 = new \vo\Patient(); $p1->name = "Patient 1"; $p1->id = 1;
$p2 = new \vo\Patient(); $p2->name = "Patient 2"; $p2->id = 2;*/

/********************************************************************/
// This is a new element
$doctor = new \vo\Doctor(); $doctor->name = "Doctor NEW";

// This is a detached element which is in the database
$patient = new \vo\Patient(); $patient->name = "Patient 1"; $patient->id = 1;

$doctor->patients->add($patient); $patient->doctor = $doctor;
/********************************************************************/

// Now replace $patient with its managed version
$managedPatient = $em->merge($patient);

echo spl_object_hash($doctor)."<br/>";
echo spl_object_hash($managedPatient->doctor)."<br/>";
var_dump($doctor === $managedPatient->doctor);

exit;

//$managedPatient->doctor = $doctor;
$doctor->patients->set(0, $managedPatient);

// Persist the doctor
$em->persist($doctor);

$em->getUnitOfWork()->computeChangeSets();

$changeSets = array("entityInsertions" => $em->getUnitOfWork()->getScheduledEntityInsertions(),
					"entityUpdates" => $em->getUnitOfWork()->getScheduledEntityUpdates(),
					"entityDeletions" => $em->getUnitOfWork()->getScheduledEntityDeletions(),
					"collectionUpdates" => $em->getUnitOfWork()->getScheduledCollectionUpdates(),
					"collectionDeletions" => $em->getUnitOfWork()->getScheduledCollectionDeletions());

var_dump($changeSets);

$em->flush();

/*
$managedP1 = $em->merge($p1);
$managedP2 = $em->merge($p2);

$d1->patients->add($managedP1); $managedP1->doctor = $d1;
$d1->patients->add($managedP2); $managedP2->doctor = $d1;

$em->persist($d1);*/
//$em->flush();

// This is a new element
/*($d1 = new \vo\Doctor(); $d1->name = "Doctor NEW";

// These are detached, but are definitely in the database
$p1 = new \vo\Patient(); $p1->name = "Patient 1"; //$p1->id = 1;
$p2 = new \vo\Patient(); $p2->name = "Patient 2"; //$p2->id = 2;

$d1->patients->add($p1); $p1->doctor = $d1;
$d1->patients->add($p2); $p2->doctor = $d1;

$em->persist($d1);
$em->persist($p1);
$em->persist($p2);

Flextrine\Tools\AssociationWalker::mergeAssociations($d1, $em);*/

//$mergedPatient = $em->merge($p1);

//$d1->patients->set(0, $em->merge($d1->patients->get(0)));
//$d1->patients->set(1, $em->merge($d1->patients->get(1)));

// Persist the new doctor
//$em->persist($d1);

//$p1=$em->merge($p1);
//$p2=$em->merge($p2);

// The only way it REALLY works is to merge the objects before building the tree.  This is complicated because some of the objects coming over the AMF wire
// will be references to other objects in the same wire.  So: I need to maintain an spl_object_hash map of original object hash to managed object hash, and
// use the managed object if it exists.

// VERSION 1: This works BECAUSE the merge is done before $p1 links to $d1
//$d1->patients->add($p1); $p1->doctor = $d1;
//$d1->patients->add($p2); $p2->doctor = $d1;

// VERSION 2: This throws an exception!

/*$d1->patients->add($p1); $p1->doctor = $d1;
$d1->patients->add($p2); $p2->doctor = $d1;

$p1 = $em->merge($p1);
$p2 = $em->merge($p2);

$d1->patients->set(0, $p1);
$d1->patients->set(1, $p2);

$em->persist($d1);
$em->flush();*/

//return;

//\Flextrine\Tools\AssociationWalker::mergeAssociations($d1, $em);

// The new workflow goes:
// Do all the persists
// Flush
// Do all the merges
// Do all the removes
// Flush

// So, do the persist and flush
//$em->persist($d1);
//$em->flush();

//\Flextrine\Tools\AssociationWalker::mergeAssociations($d1, $em);

//$em->flush();
//exit;
// Check what the changesets will be

?>